<?php
/**
 * Date: 10/28/12
 * Time: 9:53 PM
 * User: Amitav Roy
 */
class Bills_m extends CI_Model {
	// The bills base table
	protected $bills_table = 'bills';

	// The biller info table
	protected $billers_table = 'billers';

	// The reference of bills and billers
	protected $bills_billers = 'bills_billers';

	// Entity type -- imp for description
	protected $entity_type = 'bills';

	// Per page count to be set here.
	protected $per_page = '20';

	// User id
	protected $user_id;

	function __construct() {
		parent::__construct();

		$this->user_id = $this->session->userdata('uid');
	}

	/**
	 * This is the base query where I am joining all the tables
	 * for the data required to display bill data.
	 */
	protected function base_view_bills_payed() {
		$fields = array(
			'b.date',
			'b.amount',
			'd.description',
			'br.name',
			'b.bid'
		);

		$this->db->select(implode(",", $fields),FALSE);
		$this->db->from($this->bills_table . ' b');

		// Joining the description table
		$this->db->join('description d','d.eid = b.bid AND d.entity_type = "'.$this->entity_type.'"');

		// Joining the Bills and Billers table
		$this->db->join($this->bills_billers . ' bb','bb.bid = b.bid');

		// Joining the Biller table
		$this->db->join($this->billers_table . ' br','br.brid = bb.brid');
	}

	/**
	 * This is the public function which can be used to get the protected base query.
	 * The base query should not be used directly inside the class also.
	 */
	public function get_base_view_bills_payed() {
		return $this->base_view_bills_payed();
	}

	/**
	 * This is the function where I am adding extra rules
	 * before building the final query to view the bills
	 * that are already payed.
	 *
	 * This also includes a pager.
	 */
	function view_bills_payed() {

		// Calling the base query.
		$this->get_base_view_bills_payed();

		// Pagination offset
		$offset = $this->uri->segment(3, 0);
		$this->db->limit($this->per_page, $offset);

		// Setting the order by conditions
		$this->db->order_by('date','DESC');

		// Running the query
		$query = $this->db->get();

		// return $this->db->last_query();
		return $query->result_array();
	}

	/**
	 * This function is to get the number of bills in the system.
	 * Used for the pager count in pagination.
	 *
	 * @return int - record count
	 */
	function bills_payed_count() {
		$this->get_base_view_bills_payed();

		$query = $this->db->get();

		return $query->num_rows();
	}

	/**
	 * This is where I am adding the data into the system.
	 */
	function add_bill_payed($data) {
		$bill_data['uid'] = $this->user_id;
		$bill_data['amount'] = $data['amount'];
		$bill_data['date'] = $data['date'];

		// Adding the data into the bills table.
		$this->db->insert($this->bills_table, $bill_data);

		// Getting the bill id as entity id for description table.
		$bill_id = $this->db->insert_id();

		// Adding the description
		$description = $data['description'];
		$this->global_model->global_add_description($description, $bill_id, $this->entity_type);

		// Adding data to bills_biller table
		$bills_billers['brid'] = $data['brid'];
		$bills_billers['bid'] = $bill_id;

		$this->db->insert($this->bills_billers,$bills_billers);
	}
}

/* End of file bills_m.php */
/* Location: ./application/controllers/bills_m.php */